<?php

namespace fast;


use think\Exception;

class Express100
{

//    const KEY ='LsANqNnc5885';
//    const CUSTOMER ='90745DC921842964F2DAE9EF99998ADC';

    const KEY = "fjLkyvDn6122";
    const CUSTOMER = "903841E3D1BDF3B77A404AB29EA1C300";

    const  URL='https://poll.kuaidi100.com/poll/query.do';

    public static $call_back_url = "";


    //智能识别快递公司
    public static function getCompany($expressNo){
        //先获取快递公司 comCode
        $url = "http://m.kuaidi100.com/autonumber/auto?num=".$expressNo."&key=".self::KEY;
        $result = self::curl($url);
        if(empty($result)){
            return false;
        }
        $dataInfo = json_decode($result,true);
        if(empty($dataInfo) || !is_array($dataInfo)){
            return false;
        }
        if(isset($dataInfo['message'])){
            throw new Exception($dataInfo['message']);
        }
        if(empty($dataInfo[0]['comCode'])){
            return false;
        }
        return $dataInfo;
    }

    /**  获取物流信息
     * @param $expressNo  快递单号
     * @param string $com   快递公司编号
     * @return bool|mixed
     */
    public static function getExpressInfo($expressNo,$com='')
    {
        if(empty($com)){
            $dataInfo = self::getCompany($expressNo);
            if(empty($dataInfo)){
                return false;
            }
            $com = $dataInfo[0]['comCode'];
        }
        $json=json_encode(['com'=>$com,'num'=>$expressNo,'phone' => '', 'from' => '','to' => '','resultv2' => '1'],true);
        $post_data=[
            'customer'=>self::CUSTOMER,
            'param'=>$json
        ];
        $post_data['sign']=strtoupper(md5($json.self::KEY.self::CUSTOMER));
        $params = "";
        foreach ($post_data as $k=>$v) {
            $params .= "$k=".urlencode($v)."&";		//默认UTF-8编码格式
        }
        $post_data = substr($params, 0, -1);
        $url = "https://poll.kuaidi100.com/poll/query.do";
        return self::curl_post($post_data,$url);
    }


    //快递信息订阅
    public static function subscribe($company,$number,$from='',$to=''){
        $salt = md5(microtime(true));
        $autoCom = $company?0:1;
        $param = array (
            'company' => $company,             // 快递公司编码
            'number' => $number,      // 快递单号
            'from' => $from,                     // 出发地城市
            'to' => $to,                       // 目的地城市
            'key' => self::KEY,                    // 客户授权key
            'parameters' => array (
                'callbackurl' => self::$call_back_url,          // 回调地址
                'salt' => $salt,                 // 加密串
                'resultv2' => '4',            // 行政区域解析
                'autoCom' => $autoCom,             // 单号智能识别
                'interCom' => '0',            // 开启国际版
                'departureCountry' => '',     // 出发国
                'departureCom' => '',         // 出发国快递公司编码
                'destinationCountry' => '',   // 目的国
                'destinationCom' => '',       // 目的国快递公司编码
                'phone' => ''                 // 手机号
            )
        );

        // 请求参数
        $post_data = array();
        $post_data['schema'] = 'json';
        $post_data['param'] = json_encode($param, JSON_UNESCAPED_UNICODE);

        $url = 'https://poll.kuaidi100.com/poll';    // 订阅请求地址
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        return  json_decode($result, true);
    }


    public static function curl_post($post_data,$url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        return json_decode($result,true);
    }

    public static function curl($url)
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "",
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            return "cURL Error #:" . $err;
        } else {
            return $response;
        }
    }






}
